﻿using System;
using Dost.Khcn.Data.EntityModel;
using Dost.Khcn.Data.Implements;
using Dost.Khcn.Data.Interfaces;
using Dost.Khcn.Model;
using Monkey.Framework.Logging;

namespace Dost.Khcn.Biz
{
    public class EmployeeBiz
    {
        #region Properties

        private IEmployeeDao _employeeDao;

        private IEmployeeDao EmployeeData
        {
            get { return _employeeDao ?? (_employeeDao = new EmployeeDao()); }
        }

        #endregion

        #region Constructors

        public EmployeeBiz()
        {
            //_employeeDao = new EmployeeDao();
        }

        public EmployeeBiz(IEmployeeDao employeeDao)
        {
            _employeeDao = employeeDao;
        }

        #endregion

        #region Methods

        public Employee Logon(string userName, string password)
        {
            return EmployeeData.GetEmployeeByUserNamePassword(userName, password);
        }

        public Employee GetById(int id)
        {
            if (id <= 0) return null;
            try
            {
                return EmployeeData.GetById(id);
            }
            catch (Exception ex)
            {
                LoggingFactory.GetLogger().Log(ex.ToString());
            }

            return null;
        }

        public Employee CreateEmployee(Employee employee)
        {
            // validate inputs
            if (employee == null) return null;
            if (string.IsNullOrEmpty(employee.Code) || string.IsNullOrEmpty(employee.Name)) return null;

            // is existed username
            var existedEmp = EmployeeData.GetByUserName(employee.UserName);
            if (existedEmp != null)
                throw new ApplicationException("UserNameIsExisted");

            if (employee.Role != UserRole.Admin)
            {
                employee.IsAdmin = false;
            }

            try
            {
                return EmployeeData.AddEmployee(employee);
            }
            catch (Exception ex)
            {
                LoggingFactory.GetLogger().Log(ex.ToString());
                return null;
            }
        }

        public int GetMaxId()
        {
            return EmployeeData.GetMaxId();
        }

        public int GetNextCode()
        {
            return EmployeeData.GetCurrentIdentity() + 1;
        }

        public void UpdateEmployee(Employee employee)
        {
            if (employee == null) return;

            if (employee.Role != UserRole.Admin)
            {
                employee.IsAdmin = false;
            }

            EmployeeData.UpdateEmployee(employee);
        }

        public object GetAllEmployeesWs(int startIndex, int pageSize, string sorting, string name, int roleId)
        {
            try
            {
                int totalRecords;
                var peopleList = EmployeeData.GetEmployees(startIndex, pageSize, sorting, name, roleId, out totalRecords);
                return new { Result = "OK", Records = peopleList, TotalRecordCount = totalRecords };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", ex.Message };
            }
        }

        public object CreateEmployeeWs(Employee employee)
        {
            // validate inputs
            if (employee == null) return null;
            if (string.IsNullOrEmpty(employee.Code) || string.IsNullOrEmpty(employee.Name)) return null;

            try
            {
                var addedEmployee = EmployeeData.AddEmployee(employee);
                return new { Result = "OK", Record = addedEmployee };
            }
            catch (Exception ex)
            {
                //throw;
                return new { Result = "ERROR", ex.Message };
            }
        }

        public object UpdateEmployeeWs(Employee employee)
        {
            if (employee == null) return null;
            try
            {
                EmployeeData.UpdateEmployee(employee);
                return new { Result = "OK" };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", ex.Message };
            }
        }

        public object DeleteEmployeeWs(int employeeId)
        {
            try
            {
                EmployeeData.DeleteEmployee(employeeId);
                return new { Result = "OK" };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", ex.Message };
            }
        }

        public Employee GetByName(string userName)
        {
            return EmployeeData.GetByUserName(userName);
        }

        #endregion

        #region Helpers

        #endregion
    }
}
